//
//  FHNewFMHeadView.m
//  ZhiXinFM
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 YITeam. All rights reserved.
//

#import "FHNewFMHeadView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "FHScorllViewUrl.h"
#import "FHNewFmModels.h"

#define NEWFMVIEWCOUNT 4
@interface FHNewFMHeadView ()
@property(nonatomic,strong)NSMutableArray *arrayNewFm;

@end
@implementation FHNewFMHeadView
+ (instancetype)newFmWithTableView:(UITableView *)tableview{
    static NSString *newFm=@"fm";
    FHNewFMHeadView *headNewFm=[tableview dequeueReusableCellWithIdentifier:newFm];
    if (headNewFm==nil) {
        headNewFm=[[FHNewFMHeadView alloc]initWithReuseIdentifier:newFm];
    }
    return headNewFm;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithReuseIdentifier:reuseIdentifier]) {
        UIView *newFmView=[[UIView alloc]init];
        newFmView.backgroundColor=[UIColor redColor];
        [self.contentView addSubview:newFmView];
        self.parentView=newFmView;
    }
    [self requestNewFmInfo];
    return self;
}
- (void)requestNewFmInfo{
    AFHTTPRequestOperationManager *mgr=[[AFHTTPRequestOperationManager alloc]init];
    [mgr GET:NEWFMINFO parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayNewFm=[NSMutableArray array];
        for (NSDictionary * dic in [responseObject objectForKey:@"data"]) {
            FHNewFmModels *models=[FHNewFmModels newFmWithDic:dic];
            [self.arrayNewFm addObject:models];
        }
        NSLog(@"%@",self.arrayNewFm);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.parentView.frame=CGRectMake(0, 40, self.bounds.size.width, self.bounds.size.height);
    /*int totalColumns = 2;
    
    // 1.应用的尺寸
    CGFloat appW = 175;
    CGFloat appH = 200;
    
    // 2.间隙 = (控制器view的宽度 - 3 * 应用宽度) / 4
    CGFloat marginX = (self.frame.size.width - totalColumns * appW) / (totalColumns + 1);
    CGFloat marginY = 15;
    
    // 3.根据应用个数创建对应的框框(index 0 ~ 11)
    for (int index = 0; index<NEWFMVIEWCOUNT; index++) {
        // 3.1.创建1小框框
        UIView *appView = [[UIView alloc] init];

        // 3.2.计算框框的位置
        // 计算行号和列号
        int row = index / totalColumns;
        int col = index % totalColumns;
        // 计算x和y
        CGFloat appX = marginX + col * (appW + marginX);
        CGFloat appY =  row * (appH + marginY);
        // 设置frame
        appView.frame = CGRectMake(appX, appY, appW, appH);
        appView.backgroundColor=[UIColor yellowColor];
        // 3.3.添加框框到控制器的view
        [self.parentView addSubview:appView];
        
        //添加图片
        UIImageView *iconView = [[UIImageView alloc] init];
        // 设置位置
        CGFloat iconW = appW;
        CGFloat iconH = appH-40;
        CGFloat iconX = (appW - iconW) * 0.5;
        CGFloat iconY = 0;
        iconView.backgroundColor=[UIColor blueColor];
        iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
        // 设置图片
        iconView.image = [UIImage imageNamed:@"0.jpg"];
        [appView addSubview:iconView];

        // 3.4.2.添加节目名字
        UILabel *titleLabel = [[UILabel alloc] init];
        // 设置位置
        CGFloat titleW = appW;
        CGFloat titleH = 25;
        CGFloat titleX = 0;
        CGFloat titleY = iconY + iconH;
        titleLabel.frame = CGRectMake(titleX, titleY, titleW,titleH);
        titleLabel.backgroundColor=[UIColor purpleColor];
        // 设置文字
        titleLabel.text = @"张爱玲:女人如猫";
        // 设置字体
        titleLabel.font = [UIFont systemFontOfSize:13];
        [appView addSubview:titleLabel];

    
        //添加主播名字
        UILabel *nameLabel = [[UILabel alloc] init];
        // 设置位置
        CGFloat nameW = appW;
        CGFloat nameH = 15;
        CGFloat nameX = 0;
        CGFloat nameY = titleY + titleH;
        nameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
        nameLabel.backgroundColor=[UIColor blueColor];
        // 设置文字
        nameLabel.text = @"主播:沙朵";
        // 设置字体
        nameLabel.font = [UIFont systemFontOfSize:12];
        [appView addSubview:nameLabel];
        //self.newFmheight=405;//CGRectGetMinY(nameLabel.frame)+20;
       // NSLog(@"%f",self.newFmheight);
    }*/
    CGFloat infoW=self.parentView.bounds.size.width/2-10;
    UIView *infoView=[[UIView alloc]initWithFrame:CGRectMake(5, 10, infoW, 100)];
    infoView.backgroundColor=[UIColor yellowColor];
    [self.parentView addSubview:infoView];
    
    UIView *infoView1=[[UIView alloc]initWithFrame:CGRectMake(infoW+5, 10, infoW, 100)];
    infoView1.backgroundColor=[UIColor yellowColor];
    [self.parentView addSubview: infoView1];
}
@end
